package com.micrometa.km.ui;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceGeminiLookAndFeel;

import com.micrometa.km.ui.components.ToolBar;
import com.micrometa.km.ui.components.TreePanel;
import com.micrometa.km.ui.skins.Constants;
import com.micrometa.km.util.PropertyUtil;


public class MainFrame {
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				boolean hasLafSpecified = false;
				try {
					hasLafSpecified = (System.getProperty("swing.defaultlaf") != null);
				} catch (Throwable t) {
					// JNLP sandbox
				}
				try {
					if (!hasLafSpecified) {
						LookAndFeel laf = new SubstanceGeminiLookAndFeel();
						UIManager.setLookAndFeel(laf);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				//look&feel
				SubstanceLookAndFeel.setToUseConstantThemesOnDialogs(true);
				UIManager.put(SubstanceLookAndFeel.TABBED_PANE_CLOSE_BUTTONS_PROPERTY,Boolean.TRUE);
				UIManager.put(SubstanceLookAndFeel.SHOW_EXTRA_WIDGETS,Boolean.TRUE);
				String skinString=PropertyUtil.skin();
				int skinIndex=(skinString!=null&&!"".equals(skinString))?Integer.parseInt(skinString):0;
				SubstanceLookAndFeel.setSkin(Constants.getSkin(skinIndex));
				//border
				JFrame.setDefaultLookAndFeelDecorated(true);
				JDialog.setDefaultLookAndFeelDecorated(true);
				UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
				
				//main frame
				final JFrame f=new JFrame();
//				f.setAlwaysOnTop(true);
				f.setTitle("知识管理v1.0");
				f.setLayout(new BorderLayout());
				//add components
				JPanel mainPanel=new JPanel();
				mainPanel.setLayout(new BorderLayout());
				mainPanel.add(new ToolBar("", 22, true),BorderLayout.NORTH);
				mainPanel.add(new TreePanel(),BorderLayout.CENTER);
				f.add(mainPanel,BorderLayout.CENTER);
				//frame settings
				f.addComponentListener(new ComponentAdapter() {
					@Override
					public void componentResized(ComponentEvent e) {
						super.componentResized(e);
						((JFrame) e.getComponent()).getRootPane().repaint();
					}
				});
				final ClassLoader cl = Thread.currentThread().getContextClassLoader();
				if (UIManager.getLookAndFeel() instanceof SubstanceLookAndFeel) {
					URL url=Thread.currentThread().getContextClassLoader().getResource("resource/icons/logo/text_document.png");
					Image logo = Toolkit.getDefaultToolkit().getImage(url);
					f.setIconImage(logo);
				}
				//set frame size
				f.setPreferredSize(new Dimension(300, 400));//初始大小
				f.setMinimumSize(new Dimension(300, 400));//最小
				f.setResizable(true);
				f.pack();
				// center the frame in the physical screen
				Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
				f.setLocation((d.width - f.getWidth()) / 2, (d.height - f.getHeight()) / 2);
				f.setVisible(true);
				
				// 如果系统支持托盘图标,将窗口放入系统托盘区
				f.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
				final TrayIcon ti;
				URL url=Thread.currentThread().getContextClassLoader().getResource("resource/icons/logo/text_document.png");
				Image image = Toolkit.getDefaultToolkit().getImage(url);
				PopupMenu popupTi = new PopupMenu();// 弹出菜单
				MenuItem showItem = new MenuItem("显示程序");// 菜单项
				MenuItem exitItem = new MenuItem("关闭程序");// 菜单项
				ti = new TrayIcon(image, "TrayIcon", popupTi);// 图标，标题，右键弹出菜单
				ActionListener showListener = new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						f.setVisible(true);// 重新显示窗口
						SystemTray.getSystemTray().remove(ti);// 从系统托盘中移出
					}
				};
				showItem.addActionListener(showListener);
				ActionListener exitListener = new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						System.exit(0);
					}
				};
				exitItem.addActionListener(exitListener);
				popupTi.add(exitItem);
				popupTi.add(showItem);
				ti.addActionListener(showListener);// 双击显示程序
				f.addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent e) {
						if (SystemTray.isSupported()) {
							f.setVisible(false);// 隐藏窗口
							SystemTray tray = SystemTray.getSystemTray();
							try {
								tray.add(ti);// 在系统托盘区中增加图标
							} catch (AWTException ee) {
								System.err.println(ee);
							}
						} else {
							System.exit(0);// 否则结束程序
						}
					}
				});
			}
		});
	}
}
